package com.example.housingRentalSystem.controller;

import com.example.housingRentalSystem.bean.Order;
import com.example.housingRentalSystem.service.OrderService;
import com.example.housingRentalSystem.utils.ApiResponse;
import com.example.housingRentalSystem.utils.HttpStatus;
import com.example.housingRentalSystem.utils.PageRequest;
import com.example.housingRentalSystem.utils.PageResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @PostMapping("/createOrder")
    public ApiResponse<Order> createOrder(@RequestBody Order order) {
        Order createdOrder = orderService.createOrder(order);
        return ApiResponse.success(HttpStatus.OK.getCode(), "订单创建成功", createdOrder);
    }

    @PostMapping("/payOrder/{orderId}/{houseId}")
    public ApiResponse<Void> payOrder(@PathVariable Integer orderId,@PathVariable Integer houseId) {
        orderService.processPayment(orderId, houseId);
        return ApiResponse.success(HttpStatus.OK.getCode(), "支付成功");
    }

    @PutMapping("/confirmOrder/{orderId}")
    public ApiResponse<Void> confirmOrder(@PathVariable Integer orderId) {
        orderService.confirmOrder(orderId);
        return ApiResponse.success(HttpStatus.OK.getCode(), "确认成功");
    }

    @GetMapping("/listOrders")
    public ApiResponse<PageResponse<Order>> listOrders(
            @RequestParam(required = false) Integer customerId,
            @RequestParam(required = false) Integer ownerId,
            @RequestParam(required = false) Integer houseId,
            @RequestParam(required = false) Integer status,
            @ModelAttribute PageRequest pageRequest) { // 新增分页参数

        // 执行参数校验
        pageRequest.validate();

        return ApiResponse.success(
                HttpStatus.OK.getCode(),
                "查询成功",
                orderService.searchOrders(customerId, ownerId, houseId, status, pageRequest)
        );
    }

    @GetMapping("/getOrderDetail/{id}")
    public ApiResponse<Order> getOrderDetail(@PathVariable Integer id) {
        Order order = orderService.getOrderById(id);
        return ApiResponse.success(HttpStatus.OK.getCode(), "查询成功", order);
    }
    @PutMapping("/cancelOrder/{orderId}")
    public ApiResponse<Void> cancelOrder(
            @PathVariable Integer orderId,
            @RequestBody Map<String, String> requestBody) {

        String reason = requestBody.get("reason");
        try {
            orderService.cancelOrder(orderId, reason);
            return ApiResponse.success(HttpStatus.OK.getCode(), "订单取消成功");
        } catch (IllegalArgumentException e) {
            return ApiResponse.error(HttpStatus.BAD_REQUEST.getCode(), e.getMessage());
        } catch (RuntimeException e) {
            return ApiResponse.error(HttpStatus.INTERNAL_SERVER_ERROR.getCode(), e.getMessage());
        }
    }

    @PutMapping("/agreeOrder/{orderId}")
    public ApiResponse<Void> agreeOrder(@PathVariable Integer orderId) {
        orderService.agreeOrder(orderId);
        return ApiResponse.success(HttpStatus.OK.getCode(), "退租成功");
    }
    @PutMapping("/refuseOrder")
    public ApiResponse<Void> refuseOrder(Integer orderId) {
        orderService.refuseOrder(orderId);
        return ApiResponse.success(HttpStatus.OK.getCode(), "拒绝退租");
    }

    @PostMapping("/refuseOrder/{orderId}")
    public ApiResponse<Void> refuseOrder(
            @PathVariable Integer orderId,
            @RequestBody Map<String, String> requestBody) {

        String refusalReason = requestBody.get("refusalReason");
        try {
            orderService.refuseOrder(orderId, refusalReason);
            return ApiResponse.success(HttpStatus.OK.getCode(), "退租拒绝成功");
        } catch (IllegalArgumentException e) {
            return ApiResponse.error(HttpStatus.BAD_REQUEST.getCode(), e.getMessage());
        }
    }
}